package com.molichuxing.gateway.utils;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSONObject;
import com.molichuxing.gateway.utils.contants.RequestAttrConst;
import com.molichuxing.gateway.utils.entity.DealerEntity;

/**
 * request获取参数工具类
 * 
 * @author zoumingyu
 * @date 2019年9月9日
 *
 */
public class RequestAttrUtil {

	/**
	 * 获取当前经销商用户关联经销商信息
	 *
	 * @return
	 */
	public static DealerEntity getCurrentDealer() {
		Object obj = getAttributeValue(RequestAttrConst.DEALER_KEY);
		return obj == null ? null : JSONObject.parseObject(JSON.toJSONString(obj), DealerEntity.class);
	}

	/**
	 * 获取当前经销商用户关联经销商id
	 *
	 * @return
	 */
	public static Integer getCurrentDealerId() {
		DealerEntity dealer = getCurrentDealer();
		return dealer == null ? null : dealer.getId();
	}

	private static Object getAttributeValue(String name) {
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
				.currentRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();

		return request.getAttribute(name);
	}
}
